package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.enums.QuestionTypeEnum;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.mapper.ExamQuestionMapper;
import cn.wolfcode.mapper.QuestionItemMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Override
    public Long save(Exam exam) {
        //在试卷表里面插入试卷信息
        examMapper.insert(exam);
        //获取题库中的单选题
        List<Question> single_questions = questionMapper.selectByType(QuestionTypeEnum.SINGLE.getValue());
        //获取题库中的多选题
        List<Question> multiple_questions = questionMapper.selectByType(QuestionTypeEnum.MUTIPLE.getValue());
        //获取题库中的判断
        List<Question> judge_questions = questionMapper.selectByType(QuestionTypeEnum.JUDGE.getValue());
        //将题库中的题目打乱
        Collections.shuffle(single_questions);
        Collections.shuffle(multiple_questions);
        Collections.shuffle(judge_questions);
        //获取时间中的单选题数量
        Integer single_amount = exam.getSingle_Amount();
        Integer multiple_amount = exam.getMultiple_Amount();
        Integer judge_amount = exam.getJudge_Amount();
        int count = 1;
        ArrayList<ExamQuestion> temp = new ArrayList<>();
        //往试卷表中添加单选题
        for (int i = 0; i < single_amount;i++){
            ExamQuestion eq = new ExamQuestion();
            eq.setExamId(exam.getId());
            //设置分数
            eq.setScore(exam.getSingle_Acore());
            //设置序列号
            eq.setSequence(count);
            //设置单选题id
            eq.setQuestionId(single_questions.get(i).getId());
            count++;
            temp.add(eq);
        }
        for (int i = 0; i < multiple_amount;i++){
            ExamQuestion eq = new ExamQuestion();
            eq.setExamId(exam.getId());
            //设置分数
            eq.setScore(exam.getMultiple_Score());
            //设置序列号
            eq.setSequence(count);
            //设置单选题id
            eq.setQuestionId(multiple_questions.get(i).getId());
            count++;
            temp.add(eq);
        }
        for (int i = 0; i < judge_amount;i++){
            ExamQuestion eq = new ExamQuestion();
            eq.setExamId(exam.getId());
            //设置分数
            eq.setScore(exam.getJudge_Score());
            //设置序列号
            eq.setSequence(count);
            //设置单选题id
            eq.setQuestionId(judge_questions.get(i).getId());
            count++;
            temp.add(eq);
        }
        //进行批量插入
        if (temp.size()>0) {
            examQuestionMapper.batchInsert(temp);
        }
        return exam.getId();
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        return null;
    }

    @Override
    public Exam show(Long id) {
        //查询本次考试信息
        Exam exam = examMapper.selectByPrimaryKey(id);
        return exam;
    }
}
